package com.lumingfeng.serviceImpl;

import com.lumingfeng.dao.GoodsDao;
import com.lumingfeng.entity.Goods;
import com.lumingfeng.entity.InDetail;
import com.lumingfeng.service.GoodsService;
import com.lumingfeng.service.InDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yc on 16/8/16.
 */
@Service(value = "goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;
    @Resource
    private InDetailService inDetailService;

    public Integer insertGoods(Goods goods) {
        return goodsDao.insertGoods(goods);
    }

    public Goods getGoodsById(int id) {
        return goodsDao.getGoodsById(id);
    }

    public List<Goods> getGoods() {
        return goodsDao.getGoods();
    }

    public List<Goods> getGoodsByName(String name) {
        return goodsDao.getGoodsByName(name);
    }

    public List<Goods> getRTGoods() {
        List<Goods> goodsList = goodsDao.getGoods();
        return filterRTGoods(goodsList);
    }

    public List<Goods> getRTGoodsByName(String name) {
        List<Goods> goodsList = goodsDao.getGoods();
        return filterRTGoods(goodsList);
    }


    public List<Goods> filterRTGoods(List<Goods> goodsList) {
        List<Goods> RTGoodsList = new ArrayList<Goods>();
        for (Goods goods : goodsList) {
            int allCount = 0;
            List<InDetail> inDetailList = inDetailService.getRTInDetailsByGoodsId(goods.getId());
            for (InDetail inDetail : inDetailList) {
                allCount = allCount + inDetail.getLeftcount();
            }

            if (allCount > 0) {
                RTGoodsList.add(goods);
            }
        }

        return RTGoodsList;
    }


//    public int deleteByPrimaryKey(Integer id) {
//        return goodsDao.deleteByPrimaryKey(id);
//    }
//
//    public int insert(Goods record) {
//        return goodsDao.insert(record);
//    }
//
//    public Goods selectByPrimaryKey(Integer id) {
//        return goodsDao.selectByPrimaryKey(id);
//    }
//
//    public int updateByPrimaryKey(Goods record) {
//        return goodsDao.updateByPrimaryKey(record);
//    }
}
